package com.egao.appo.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.appo.biz.PayRecordBiz;
import com.egao.appo.entity.PayRecord;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-07-23 09:43:58
 * @version ：1.0
 */
@Repository
public class PayRecordBizImpl extends BaseDao implements PayRecordBiz {

	@Override
	public JsonResult list(PageResult2<T> page, PayRecord payRecord, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PayRecord> rm = new BeanPropertyRowMapper<PayRecord>(PayRecord.class);
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.pay_user_id");
    sql.append(",p.type,p.fee_name,p.price,p.operate_user_id,p.remark");
		sql.append(",p.order_num,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
    sql.append(",payUser.nickname payUserName, operateUser.nickname operateUserName ");
    sql.append(" from fx_fee_record p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_user payUser on payUser.user_id=p.pay_user_id ");
    sql.append(" inner join sys_user operateUser on operateUser.user_id=p.operate_user_id ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(payRecord)){
			if(Utils.isNotEmpty(payRecord.getPayUserName())){
				sql.append(" and instr(payUser.nickname,:payUserName)>0 ");
				map.put("payUserName", payRecord.getPayUserName());
			}
    if(Utils.isNotEmpty(payRecord.getSort())&&Utils.isNotEmpty(payRecord.getOrder())){
        sql.append(" order by p."+payRecord.getSort()+" "+payRecord.getOrder());
      } else {
        sql.append(" order by p.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<PayRecord> list(PayRecord payRecord, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PayRecord> rm = new BeanPropertyRowMapper<PayRecord>(PayRecord.class);
    StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.pay_user_id");
    sql.append(",p.type,p.fee_name,p.price,p.operate_user_id,p.remark");
		sql.append(",p.order_num,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
		sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
		sql.append(",payUser.nickname payUserName, operateUser.nickname operateUserName ");
		sql.append(" from fx_fee_record p ");
		sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
		sql.append(" inner join sys_user payUser on payUser.user_id=p.pay_user_id ");
		sql.append(" inner join sys_user operateUser on operateUser.user_id=p.operate_user_id ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(payRecord)){
			if(Utils.isNotEmpty(payRecord.getPayUserName())){
				sql.append(" and instr(payUser.nickname,:payUserName)>0 ");
				map.put("payUserName", payRecord.getPayUserName());
			}
      if(Utils.isNotEmpty(payRecord.getSort())&&Utils.isNotEmpty(payRecord.getOrder())){
				sql.append(" order by p."+payRecord.getSort()+" "+payRecord.getOrder());
			} else {
				sql.append(" order by p.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public PayRecord getById(PayRecord payRecord) {
		RowMapper<PayRecord> rm = new BeanPropertyRowMapper<PayRecord>(PayRecord.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.pay_user_id");
    sql.append(",p.type,p.fee_name,p.price,p.operate_user_id,p.remark");
		sql.append(",p.order_num,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
		sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
		sql.append(",payUser.nickname payUserName, operateUser.nickname operateUserName ");
		sql.append(" from fx_fee_record p ");
		sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
		sql.append(" inner join sys_user payUser on payUser.user_id=p.pay_user_id ");
		sql.append(" inner join sys_user operateUser on operateUser.user_id=p.operate_user_id ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 and p.id=:id ");
		map.put("id", payRecord.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(PayRecord payRecord, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into fx_fee_record (");
    sql.append("organization_id,pay_user_id,type,fee_name,price");
		sql.append(",operate_user_id,remark,order_num,create_user,create_time) values ( ");
    sql.append(":organization_id,:pay_user_id,:type,:fee_name,:price");
		sql.append(",:operate_user_id,:remark,:order_num,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("pay_user_id", payRecord.getPayUserId()+"");
    map.put("type", payRecord.getType());
    map.put("fee_name", payRecord.getFeeName());
    map.put("price", payRecord.getPrice()+"");
    map.put("operate_user_id", payRecord.getOperateUserId()==null?null:payRecord.getOperateUserId()+"");
    map.put("remark", payRecord.getRemark());
    map.put("order_num", payRecord.getOrderNum()==null?null:payRecord.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(PayRecord payRecord, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update fx_fee_record set ");
    sql.append("pay_user_id=:pay_user_id,type=:type,fee_name=:fee_name,price=:price ");
		sql.append(",operate_user_id=:operate_user_id,remark=:remark,order_num=:order_num ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("pay_user_id", payRecord.getPayUserId()+"");
    map.put("type", payRecord.getType());
    map.put("fee_name", payRecord.getFeeName());
    map.put("price", payRecord.getPrice()+"");
    map.put("operate_user_id", payRecord.getOperateUserId()==null?null:payRecord.getOperateUserId()+"");
    map.put("remark", payRecord.getRemark());
    map.put("order_num", payRecord.getOrderNum()==null?null:payRecord.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", payRecord.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(PayRecord payRecord, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(payRecord.getId().length()>0){
			for(String ID : payRecord.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update fx_fee_record set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
